package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreAddress;
import com.xiaoyu.lingdian.entity.CoreUser;
import com.xiaoyu.lingdian.service.CoreAddressService;
import com.xiaoyu.lingdian.service.CoreUserService;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CoreAddressVO;

@Controller
@RequestMapping(value="/coreAddress")
public class CoreAddressController extends BaseController {

	/**
	 * 收货地址表
	 */
	@Autowired
	private CoreAddressService coreAddressService;
	
	/**
	 * 用户表
	 */
	@Autowired
	private CoreUserService coreUserService;
	
	/**
	 * 根据收货人分页查询收货地址列表
	 * 
	 * @param crarsName
	 * @param pageNum
	 * @param pageSize
	 * @param response
	 */
	@RequestMapping(value = "/find/by/cnd", method = RequestMethod.POST)
	public void findByCnd(String crarsName, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CoreAddressController.findByCnd]:begin findByCnd.");
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		Page<CoreAddress> page = coreAddressService.findCoreAddressByCnd(null, crarsName, pageNum, pageSize);
		Page<CoreAddressVO> pageVO = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());		
		List<CoreAddressVO> list = new ArrayList<>();
		CoreAddressVO vo = new CoreAddressVO();
		for (CoreAddress coreAddress : page.getResult()) {
			vo = new CoreAddressVO();
			vo.convertPOToVO(coreAddress);
			list.add(vo);
		}
		pageVO.setResult(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询收货地址列表成功", pageVO), response);
		logger.info("[CoreAddressController.findByCnd]:end findByCnd.");
	}
	
	/**
	 * 我的收货地址
	 * 
	 * @param crarsUser
	 * @param response
	 */
	@RequestMapping(value = "/find/my/address", method = RequestMethod.POST)
	public void findMyAddress(String crarsUser, HttpServletResponse response) {
		logger.info("[CoreAddressController.findMyAddress]:begin findMyAddress.");
		if (StringUtils.isBlank(crarsUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "crarsUser不能为空！"), response);
			return;
		}
		List<CoreAddress> list = coreAddressService.findCoreAddressByMy(crarsUser);
		List<CoreAddressVO> listVO = new ArrayList<>();
		CoreAddressVO vo = new CoreAddressVO();
		for (CoreAddress coreAddress : list) {
			vo = new CoreAddressVO();
			vo.convertPOToVO(coreAddress);			
			CoreUser coreUser = new CoreUser();
			coreUser.setCrusrUuid(coreAddress.getCrarsUser());
			coreUser = coreUserService.getCoreUser(coreUser);
			if (coreUser != null) {
				vo.setCrarsUserName(coreUser.getCrusrName());	
			}
			listVO.add(vo);
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询我的收货地址成功", listVO), response);
		logger.info("[CoreAddressController.findMyAddress]:end findMyAddress.");
	}
	
	/**
	 * 添加收货地址
	 * 
	 * @param crarsUser
	 * @param crarsName
	 * @param crarsTel
	 * @param crarsMail
	 * @param crarsCity
	 * @param crarsAddress
	 * @param crarsDefault 是否默认(1默认2不默认）
	 * @param response
	 */
	@RequestMapping(value="/add/address", method=RequestMethod.POST)
	public void addAddress(String crarsUser, String crarsName, String crarsTel, String crarsMail, String crarsCity, 
			String crarsAddress, Integer crarsDefault, HttpServletResponse response) {
		logger.info("[CoreAddressController.addAddress]:begin addAddress");	
		if (StringUtils.isBlank(crarsUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "用户UUID不能为空！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入收货人姓名！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入联系方式！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsCity)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入所在省市！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsAddress)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入详细地址！"), response);
			return;
		}
		if (null == crarsDefault || 0 == crarsDefault) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请确认是否默认！"), response);
			return;
		}
		if (1 == crarsDefault) {
			List<CoreAddress> list = coreAddressService.findCoreAddressByMy(crarsUser);
			for (CoreAddress coreAddress : list) {
				CoreAddress address = new CoreAddress();
				address = coreAddress;
				address.setCrarsDefault(2);
				coreAddressService.updateCoreAddress(address);
			}
		}
		CoreAddress coreAddress = new CoreAddress();
		String uuid = RandomUtil.generateString(16);
		coreAddress.setCrarsUuid(uuid);
		coreAddress.setCrarsCity(crarsCity);
		coreAddress.setCrarsAddress(crarsAddress);
		coreAddress.setCrarsMail(crarsMail);
		coreAddress.setCrarsName(crarsName);
		coreAddress.setCrarsTel(crarsTel);
		coreAddress.setCrarsDefault(crarsDefault);
		coreAddress.setCrarsUser(crarsUser);
		coreAddressService.insertCoreAddress(coreAddress);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "添加收货地址成功！",uuid), response);
		logger.info("[CoreAddressController.addAddress]:end addAddress");
	}
	
	/**
	 * 编辑收货地址
	 * 
	 * @param crarsUuid
	 * @param crarsUser
	 * @param crarsName
	 * @param crarsTel
	 * @param crarsMail
	 * @param crarsCity
	 * @param crarsAddress
	 * @param crarsDefault 是否默认(1默认2不默认）
	 * @param response
	 */
	@RequestMapping(value="/update/address", method=RequestMethod.POST)
	public void updateAddress(String crarsUuid, String crarsUser, String crarsName, String crarsTel, String crarsMail, String crarsCity, 
			String crarsAddress, Integer crarsDefault, HttpServletResponse response) {
		logger.info("[CoreAddressController.updateAddress]:begin updateAddress");	
		if (StringUtils.isBlank(crarsUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[crarsUuid]参数不能为空！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "用户UUID不能为空！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入收货人姓名！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入联系方式！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsCity)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入所在省市！"), response);
			return;
		}
		if (StringUtils.isBlank(crarsAddress)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入详细地址！"), response);
			return;
		}
		if (null == crarsDefault || 0 == crarsDefault) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请确认是否默认！"), response);
			return;
		}
		if (1 == crarsDefault) {
			List<CoreAddress> list = coreAddressService.findCoreAddressByMy(crarsUser);
			for (CoreAddress coreAddress : list) {
				CoreAddress address = new CoreAddress();
				address = coreAddress;
				address.setCrarsDefault(2);
				coreAddressService.updateCoreAddress(address);
			}
		}
		CoreAddress coreAddress = new CoreAddress();
		coreAddress.setCrarsUuid(crarsUuid);
		coreAddress.setCrarsCity(crarsCity);
		coreAddress.setCrarsAddress(crarsAddress);
		coreAddress.setCrarsMail(crarsMail);
		coreAddress.setCrarsName(crarsName);
		coreAddress.setCrarsTel(crarsTel);
		coreAddress.setCrarsDefault(crarsDefault);
		coreAddressService.updateCoreAddress(coreAddress);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "编辑收货地址成功！"), response);
		logger.info("[CoreAddressController.updateAddress]:end updateAddress");
	}
	
	/**
	* 获取单个
	*
	* @param crarsUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsAddress (String crarsUuid, HttpServletResponse response) {
		logger.info("[CoreAddressController]:begin viewsAddress");

		if (StringUtil.isEmpty(crarsUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}

		CoreAddress coreAddress = new CoreAddress();
		coreAddress.setCrarsUuid(crarsUuid);

		coreAddress = coreAddressService.getCoreAddress(coreAddress);

		CoreAddressVO coreAddressVO = new CoreAddressVO();
		coreAddressVO.convertPOToVO(coreAddress);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreAddressVO), response);
		logger.info("[CoreAreaController]:end viewsAddress");

	}
	
	/**
	 * 删除收货地址（彻底删除）
	 * 
	 * @param crarsUuid
	 * @param response
	 */
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteOne(String crarsUuid, HttpServletResponse response) {
		logger.info("[CoreAddressController.deleteOne]:begin deleteOne");
		if (StringUtils.isBlank(crarsUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crarsUuid参数！"), response);
			return;
		}
		CoreAddress coreAddress = new CoreAddress();
		coreAddress.setCrarsUuid(crarsUuid);
		coreAddressService.deleteCoreAddress(coreAddress);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除收货地址成功！"), response);
		logger.info("[CoreAddressController.deleteOne]:end deleteOne");
	}
	
	/**
	* 批量删除
	*
	* @param crarsUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreAddress (String crarsUuids, HttpServletResponse response) {
		logger.info("[CoreAddressController]:begin deleteBatchCoreAddress");

		if (StringUtil.isEmpty(crarsUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}

		String[] uuids=crarsUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreAddressService.deleteCoreAddressByCnd(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreAddressController]:end deleteBatchCoreAddress");
	}
	
}